package com.ht.base.system.biz.manager.impl;

import com.ht.base.system.biz.convert.SysRoleBeanUtil;
import com.ht.base.system.biz.manager.SysRoleManager;
import com.ht.base.system.biz.manager.SysRoleMenuManager;
import com.ht.base.system.biz.mapper.SysRoleMapper;
import com.ht.base.system.common.pojo.bo.SysRoleBO;
import com.ht.base.system.common.pojo.qto.SysRoleQTO;
import com.ht.base.system.dao.dao.SysUserRoleDao;
import com.ht.base.system.dao.domain.SysRoleDO;
import com.ht.base.system.dao.domain.SysUserRoleDO;
import org.base.common.appexception.AppErrorCodeEnum;
import org.base.common.appexception.AppException;
import org.base.common.constans.SystemConstant;
import org.base.common.page.PageBO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class SysRoleManagerImpl implements SysRoleManager {
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysRoleMenuManager sysRoleMenuManager;
    @Resource
    private SysUserRoleDao sysUserRoleDao;

    @Override
    public SysRoleBO insertRole(SysRoleBO roleBO) throws AppException {
        if (roleBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isBlank(roleBO.getRoleKey())) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }

        checkParam(roleBO);

        SysRoleDO insert = SysRoleBeanUtil.sysRoleBOToDO(roleBO);
        sysRoleMapper.insetRole(insert);

        if (CollectionUtils.isNotEmpty(roleBO.getMenuIds())) {
            sysRoleMenuManager.saveRoleMenuRelation(insert.getId(), roleBO.getMenuIds());
        }

        return roleBO;
    }

    private void checkParam(SysRoleBO roleBO) {
        SysRoleDO condition = new SysRoleDO();
        condition.setRoleName(roleBO.getRoleName());
        Integer countName = sysRoleMapper.countByCondition(condition);
        if (countName != null && countName > 0) {
            throw new AppException("角色名称已被占用，请更换角色名称");
        }
        condition.setRoleName(null);
        condition.setRoleKey(roleBO.getRoleKey());

        Integer countKey = sysRoleMapper.countByCondition(condition);
        if (countKey != null && countKey > 0) {
            throw new AppException("角色权限标识已被占用，请更换角色权限标识");
        }
    }

    @Override
    public Boolean updateRole(SysRoleBO roleBO) throws AppException {
        if (roleBO == null || roleBO.getSysRoleId() == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        SysRoleDO roleDO = sysRoleMapper.findById(roleBO.getSysRoleId());
        if (roleDO == null) {
            throw new AppException("未检测到该角色信息，请刷新后重试");
        }
        if (StringUtils.isNotBlank(roleBO.getRoleName()) && !roleBO.getRoleName().equals(roleDO.getRoleName())) {
            SysRoleDO condition = new SysRoleDO();
            condition.setRoleName(roleBO.getRoleName());
            Integer countName = sysRoleMapper.countByCondition(condition);
            if (countName != null && countName > 0) {
                throw new AppException("角色名称已被占用，请更换角色名称");
            }
        }
        if (StringUtils.isNotBlank(roleBO.getRoleKey()) && !roleBO.getRoleKey().equals(roleDO.getRoleKey())) {
            SysRoleDO condition = new SysRoleDO();
            condition.setRoleKey(roleBO.getRoleKey());
            Integer countKey = sysRoleMapper.countByCondition(condition);
            if (countKey != null && countKey > 0) {
                throw new AppException("角色权限标识已被占用，请更换角色权限标识");
            }
        }

        SysRoleDO update = SysRoleBeanUtil.sysRoleBOToDO(roleBO);
        sysRoleMapper.updateRoleById(update);

        if (CollectionUtils.isNotEmpty(roleBO.getMenuIds())) {
            sysRoleMenuManager.saveRoleMenuRelation(update.getId(), roleBO.getMenuIds());
        }

        return Boolean.TRUE;
    }

    @Override
    public Boolean updateRoleStatus(SysRoleBO roleBO) throws AppException {
        if (roleBO == null || roleBO.getSysRoleId() == null || StringUtils.isBlank(roleBO.getStatus())) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_ERROR);
        }
        if (isAdmin(roleBO.getSysRoleId())) {
            throw new AppException("系统禁止修改超级管理员状态！");
        }
        SysRoleDO roleDO = new SysRoleDO();
        roleDO.setId(roleBO.getSysRoleId());
        roleDO.setStatus(roleBO.getStatus());

        sysRoleMapper.updateRoleById(roleDO);

        return Boolean.TRUE;
    }

    /**
     * 是否是admin超级管理员，是返回true
     * @param sysRoleId
     * @return
     */
    public Boolean isAdmin(Long sysRoleId) {
        SysRoleDO roleDO = sysRoleMapper.findById(sysRoleId);
        if (roleDO == null) {
            return false;
        }
        if (SystemConstant.super_admin.equals(roleDO.getRoleKey())) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean delRoleByIds(Long[] sysRoleIds) throws AppException {
        if (sysRoleIds == null || sysRoleIds.length<=0) {
            return true;
        }
        for (Long sysRoleId : sysRoleIds) {
            delRoleById(sysRoleId);
        }

        return Boolean.TRUE;
    }

    @Override
    public Boolean delRoleById(Long sysRoleId) throws AppException {
        if (sysRoleId == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        Integer count = sysUserRoleDao.countByRoleId(sysRoleId);
        if (count != null && count > 0) {
            throw new AppException("角色已被用户绑定，不能删除");
        }
        sysRoleMapper.deleteByRoleId(sysRoleId);

        return Boolean.TRUE;
    }

    @Override
    public SysRoleBO findRoleById(Long sysRoleId) throws AppException {
        return SysRoleBeanUtil.sysRoleDOToBO(sysRoleMapper.findById(sysRoleId));
    }

    @Override
    public SysRoleBO findRoleByUserId(Long sysUserId) throws AppException {
        if (sysUserId == null) {
            return null;
        }
        SysUserRoleDO byUserId = sysUserRoleDao.findByUserId(sysUserId);
        if (byUserId == null) {
            return null;
        }
        return findRoleById(byUserId.getSysRoleId());
    }

    @Override
    public PageBO<SysRoleBO> findRoleForPage(SysRoleQTO qto) throws AppException {
        PageBO<SysRoleBO> pageBO = new PageBO<>();
        if (qto == null) {
            return pageBO;
        }
        Integer count = sysRoleMapper.findForPageCount(qto);
        if (count == null || count <= 0) {
            return pageBO;
        }

        List<SysRoleBO> boList = SysRoleBeanUtil.doList2BOList(sysRoleMapper.findForPage(qto));
        pageBO.setTotal(count);
        pageBO.setData(boList);

        return pageBO;
    }
}













